<template>
	<view>
		<u--input class="input" v-model="account" fontSize="28rpx" placeholder="请输入账号"></u--input>
		<u--input class="input" v-model="password" fontSize="28rpx" placeholder="请输入密码" :password="true"></u--input>
		<u--input class="input" v-model="repassword" fontSize="28rpx" placeholder="请再次输入密码" :password="true"></u--input>

		<u-button class="button" text="注册" type="primary" v-on:click="handleRegister()"></u-button>

	</view>
</template>

<script>
	import {
		register
	} from "@/pages/user/js/register.js"

	import {
		login
	} from "@/pages/user/js/login.js"

	export default {
		data() {
			return {
				account: "",
				password: "",
				repassword: ""
			}
		},
		methods: {
			/**
			 * 处理注册
			 */
			handleRegister() {
				const account = this.account;
				const password = this.password;
				const repassword = this.repassword;
				if (account.length === 0) {
					uni.showToast({
						icon: "none",
						title: "请输入账号"
					})
				} else if (password.length === 0) {
					uni.showToast({
						icon: "none",
						title: "请输入密码"
					})
				} else if (password !== repassword) {
					uni.showToast({
						icon: "none",
						title: "两次密码输入不一致"
					})
				} else {
					register(account, password, repassword)
						.then(response => {
							uni.showToast({
								icon: "none",
								duration: 2000,
								title: "注册成功",
								complete: () => {
									let pages = getCurrentPages();
									//当前页
									let page = pages[pages.length - 1];
									//上一页
									let nextPage = pages[pages.length - 1 - 1];
									nextPage.$vm.refreshPage({
										"account": account,
										"password": password
									});
									uni.navigateBack({
										delta: 1
									});
								}
							})
						})
				}
			},
		}
	}
</script>

<style>
	page {
		padding: 24rpx;
	}

	.input {
		height: 40rpx;
		margin-bottom: 24rpx;

	}

	.button {
		margin-top: 40rpx;
		height: 80rpx;

	}
</style>
